#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/in.h>
#include <net/neighbour.h>
#include <net/arp.h>
#include <linux/if_arp.h>   /** type  */
#include <net/ip.h>
#include <net/netevent.h>
#include <linux/inetdevice.h>

/**
 *  sudo  arp-scan -I eth0  --localnet
 */

#define HBUFFERLEN 30
struct net_device *mynet_device;

int myprobe(struct platform_device *pdev) {
	int k, j;
	char hbuffer[HBUFFERLEN];
	struct in_ifaddr *ifa;
	struct net_device_stats *stats;
	const struct net_device_ops *ops;
	mynet_device = dev_get_by_name(&init_net, "eth0");
	ops = mynet_device->netdev_ops;
	if (!ops) {
		pr_err("netdev_ops is null \n");
		return -1;
	}

	pr_info("mynet_device name: %s\n", mynet_device->name);
	pr_info("mynet_device type: %d\n", mynet_device->type);

	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < mynet_device->addr_len; j++) {
		hbuffer[k++] = hex_asc_hi(mynet_device->perm_addr[j]);
		hbuffer[k++] = hex_asc_lo(mynet_device->perm_addr[j]);
		hbuffer[k++] = ':';
	}
	if (k != 0)
		--k;
	hbuffer[k] = 0;
	pr_info("mac: %s\n", hbuffer);
	ifa = mynet_device->ip_ptr->ifa_list;
	printk(KERN_INFO "ipaddr=%pI4\n", &(ifa->ifa_local));
	printk(KERN_INFO "mask=%pI4\n", &(ifa->ifa_mask));
	stats = &mynet_device->stats;
	if (!stats) {
		return -1;
	}

	dev_info(&mynet_device->dev, "tx_packets: %ld", stats->tx_packets);
	dev_info(&mynet_device->dev, "rx_packets: %ld", stats->rx_packets);

	dev_info(&mynet_device->dev, "tx_bytes: %ld", stats->tx_bytes);
	dev_info(&mynet_device->dev, "rx_bytes: %ld", stats->rx_bytes);

	dev_info(&mynet_device->dev, "tx_errors: %ld", stats->tx_errors);
	dev_info(&mynet_device->dev, "rx_errors: %ld", stats->rx_errors);

	dev_info(&mynet_device->dev, "rx_dropped: %ld", stats->rx_dropped);
	dev_info(&mynet_device->dev, "tx_dropped: %ld", stats->tx_dropped);

	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner =
THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
